<!doctype html><html lang=en dir=auto>
<head><meta charset=utf-8>
<meta http-equiv=x-ua-compatible content="IE=edge">
<meta name=viewport content="width=device-width,initial-scale=1,shrink-to-fit=no">
<meta name=robots content="index, follow">
<title>iOS | wenbin</title>
<meta name=keywords content>
<meta name=description content>
<meta name=author content>
<link rel=canonical href=https://imwenbin.com/tags/ios/>
<link crossorigin=anonymous href=/assets/css/stylesheet.min.css rel="preload stylesheet" as=style>
<link rel=icon href=https://imwenbin.com/favicon.ico>
<link rel=icon type=image/png sizes=16x16 href=https://imwenbin.com/favicon16x16.png>
<link rel=icon type=image/png sizes=32x32 href=https://imwenbin.com/favicon32x32.png>
<link rel=apple-touch-icon href=https://imwenbin.com/apple_touch_icon.png>
<link rel=mask-icon href=https://imwenbin.com/safari_pinned_tab.svg>
<meta name=theme-color content="#2e2e33">
<meta name=msapplication-TileColor content="#2e2e33">
<meta name=generator content="Hugo 0.89.3">
<link rel=alternate type=application/rss+xml href=https://imwenbin.com/tags/ios/index.xml>
<noscript>
<style>#theme-toggle,.top-link{display:none}</style>
<style>@media(prefers-color-scheme:dark){:root{--theme:rgb(29, 30, 32);--entry:rgb(46, 46, 51);--primary:rgb(218, 218, 219);--secondary:rgb(155, 156, 157);--tertiary:rgb(65, 66, 68);--content:rgb(196, 196, 197);--hljs-bg:rgb(46, 46, 51);--code-bg:rgb(55, 56, 62);--border:rgb(51, 51, 51)}.list{background:var(--theme)}.list:not(.dark)::-webkit-scrollbar-track{background:0 0}.list:not(.dark)::-webkit-scrollbar-thumb{border-color:var(--theme)}}</style>
</noscript><meta property="og:title" content="iOS">
<meta property="og:description" content>
<meta property="og:type" content="website">
<meta property="og:url" content="https://imwenbin.com/tags/ios/"><meta property="og:site_name" content="wenbin">
<meta name=twitter:card content="summary">
<meta name=twitter:title content="iOS">
<meta name=twitter:description content>
</head>
<body class=list id=top>
<script>localStorage.getItem("pref-theme")==="dark"?document.body.classList.add('dark'):localStorage.getItem("pref-theme")==="light"?document.body.classList.remove('dark'):window.matchMedia('(prefers-color-scheme: dark)').matches&&document.body.classList.add('dark')</script>
<header class=header>
<nav class=nav>
<div class=logo>
<a href=https://imwenbin.com/ accesskey=h title="wenbin (Alt + H)">wenbin</a>
<span class=logo-switches>
<button id=theme-toggle accesskey=t title="(Alt + T)"><svg id="moon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentcolor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1111.21 3 7 7 0 0021 12.79z"/></svg><svg id="sun" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentcolor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="5"/><line x1="12" y1="1" x2="12" y2="3"/><line x1="12" y1="21" x2="12" y2="23"/><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"/><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"/><line x1="1" y1="12" x2="3" y2="12"/><line x1="21" y1="12" x2="23" y2="12"/><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"/><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"/></svg>
</button>
</span>
</div>
<ul id=menu>
<li>
<a href=https://imwenbin.com/blog/ title=Blog>
<span>Blog</span>
</a>
</li>
<li>
<a href=https://imwenbin.com/archives title=Archive>
<span>Archive</span>
</a>
</li>
<li>
<a href=https://imwenbin.com/tags title=Tags>
<span>Tags</span>
</a>
</li>
</ul>
</nav>
</header>
<main class=main>
<header class=page-header><div class=breadcrumbs><a href=https://imwenbin.com/>Home</a>&nbsp;»&nbsp;<a href=https://imwenbin.com/tags/>Tags</a></div>
<h1>iOS</h1>
</header>
<article class="post-entry tag-entry">
<header class=entry-header>
<h2>制作Sqlcipher+SM4加密的framework包（OC）
</h2>
</header>
<section class=entry-content>
<p>项目中集成OpenSSL有时让人头疼，并且网上没有找到使用SM4加密数据库的案例，本文主要讲如何制作一个使用Sqlicipher+SM4国密算法的framework，本文编译部分按需求编译模拟器或真机版本
第一步 下载所需文件 GMSSL：支持国密SM2/SM3/SM4算法的OpenSSL分支，由北京大学密码研究小组维护。 GMSSL for Github
下载后的压缩包解压，使用终端cd到文件夹
输入命令 参考GMSSL编译iPhone OS目标文件
export CC=clang export CROSS_TOP=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer export CROSS_SDK=iPhoneOS.sdk export PATH="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin:$PATH" ./Configure ios64-cross no-shared --prefix=/usr/local/openssl-ios64 make 解压完后文件夹里会多两个.a文件，后面用到的有图示3个文件 第二步 新建一个项目选framework pod init
pod 'FMDB/SQLCipher' pod 'OpenSSL-Universal', '~> 1.0.2.13' 也可以使用 pod 'OpenSSL' 但是可能会报错…
至此，项目中有加密框架SQLCipher和不支持SM4的低版本OpenSSL
第三步 替换OpenSSL框架内的文件
将GMSSL编译后的如上图所示的3个文件依次替换项目中的文件
将 lib-ios 文件夹中的两个.a文件替换 将 include-ios 文件夹中的 openssl文件夹替换，替换成上图GMSSL编译后 include 文件夹中的 openssl 文件夹 Command+B 编译
可能会报错，因为openssl不支持bitcode，所以关闭项目中的bitcode检测 再次 Command+B 编译 应该顺利通过
至此，项目中所需的所有文件都有了，还需要改动一小下
第四步 打开项目中 sqlite3.c 文件 搜索 aes-256-cbc 替换成 sm4-cbc，有两处 整个项目搜索 SQLCIPHER_CRYPTO_CC 把除了 sqlite3....</p>
</section>
<footer class=entry-footer><span title="2021-11-18 09:27:03 +0800 CST">⌚️ 2021-11-18</span></footer>
<a class=entry-link aria-label="post link to 制作Sqlcipher+SM4加密的framework包（OC）" href=https://imwenbin.com/blog/%E5%88%B6%E4%BD%9Csqlcipher+sm4%E5%8A%A0%E5%AF%86%E7%9A%84framework%E5%8C%85oc/></a>
</article>
</main>
<footer class=footer>
<span>&copy; 2021 <a href=https://imwenbin.com/>wenbin</a>.</span>
<span>
保留部分权利。
</span>
</footer>
<a href=#top aria-label="go to top" title="Go to Top (Alt + G)" class=top-link id=top-link accesskey=g><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 12 6" fill="currentcolor"><path d="M12 6H0l6-6z"/></svg>
</a>
<script>let menu=document.getElementById('menu');menu&&(menu.scrollLeft=localStorage.getItem("menu-scroll-position"),menu.onscroll=function(){localStorage.setItem("menu-scroll-position",menu.scrollLeft)}),document.querySelectorAll('a[href^="#"]').forEach(a=>{a.addEventListener("click",function(b){b.preventDefault();var a=this.getAttribute("href").substr(1);window.matchMedia('(prefers-reduced-motion: reduce)').matches?document.querySelector(`[id='${decodeURIComponent(a)}']`).scrollIntoView():document.querySelector(`[id='${decodeURIComponent(a)}']`).scrollIntoView({behavior:"smooth"}),a==="top"?history.replaceState(null,null," "):history.pushState(null,null,`#${a}`)})})</script>
<script>var mybutton=document.getElementById("top-link");window.onscroll=function(){document.body.scrollTop>800||document.documentElement.scrollTop>800?(mybutton.style.visibility="visible",mybutton.style.opacity="1"):(mybutton.style.visibility="hidden",mybutton.style.opacity="0")}</script>
<script>document.getElementById("theme-toggle").addEventListener("click",()=>{document.body.className.includes("dark")?(document.body.classList.remove('dark'),localStorage.setItem("pref-theme",'light')):(document.body.classList.add('dark'),localStorage.setItem("pref-theme",'dark'))})</script>
</body>
</html>